package com.usian.controller;

import com.usian.pojo.*;
import com.usian.service.ItemCatService;
import com.usian.service.ItemParamService;
import com.usian.service.ItemService;
import com.usian.utils.CatResult;
import com.usian.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api("商品管理接口")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemCatService itemCatService;

    @Autowired
    private ItemParamService itemParamService;


    @RequestMapping("selectItemInfo")
    public TbItem selectItemInfo(Long itemId){
        return this.itemService.selectItemInfo(itemId);
    }

    /**
     * 商品列表分页查询
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("selectTbItemAllByPage")
    @ApiOperation(value ="商品列表分页查询",notes = "商品列表分页查询，默认每页10条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",type = "Integer",value = "页码"),
            @ApiImplicitParam(name = "rows",type = "Integer",value = "每页条数")
    })
    public PageResult  selectTbItemAllByPage(@RequestParam Integer page,
                                             @RequestParam Integer rows){
        return itemService.selectTbItemAllByPage(page,rows);
    }

    /**
     * 根据节点id查询商品分类子节点
     *
     * @param id
     * @return
     */
    @PostMapping("selectItemCategoryByParentId")
    @ApiOperation(value ="根据节点id查询商品分类子节点",notes = "商品分类根据节点id查询子节点")
    @ApiImplicitParam(name = "id",type = "Long",value = "主节点id")
    public List<TbItemCat> selectItemCategoryByParentId(@RequestParam Long id){
          return itemCatService.selectItemCategoryByParentId(id);
    }

    /**
     * 根据商品分类id查询对应规格参数
     *
     * @param itemCatId
     * @return
     */
    @RequestMapping("selectItemParamByItemCatId/{itemCatId}")
    public TbItemParam selectItemParamByItemCatId(@PathVariable Long itemCatId){
        return itemParamService.selectItemParamByItemCatId(itemCatId);
    }

    /**
     *
     * 商品保存
     *
     * @param item
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("insertTbItem")
    public Integer insertTbItem(@RequestBody TbItem item, @RequestParam String desc, @RequestParam String itemParams){
        return itemService.insertTbItem(item,desc,itemParams);
    }


    /**
     * 修改商品回显
     *
     * @param itemId
     * @return
     */
    @RequestMapping("preUpdateItem")
    public Map<String,Object> preUpdateItem(Long itemId){
        return itemService.preUpdateItem(itemId);
    }

    /**
     *
     * 修改商品
     *
     * @param item
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("updateTbItem")
    public Integer updateTbItem(@RequestBody TbItem item, @RequestParam String desc, @RequestParam String itemParams){
        return itemService.updateTbItem(item,desc,itemParams);
    }

    /**
     * 删除商品
     *
     * @param itemId
     * @return
     */
    @RequestMapping("deleteItemById")
    public Integer deleteItemById(@RequestParam Long itemId){
        return itemService.deleteItemById(itemId);
    }



    /**
     * 查询商品规格列表
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("selectItemParamAll")
    public PageResult selectItemParamAll(@RequestParam Integer page,
                                         @RequestParam Integer rows){
        return itemParamService.selectItemParamAll(page,rows);
    }

    /**
     * 添加商品参数模版
     *
     * @param itemCatId
     * @param paramData
     * @return
     */
    @RequestMapping("insertItemParam")
    public Integer insertItemParam(@RequestParam Integer itemCatId,@RequestParam String paramData){
        return itemParamService.insertItemParam(itemCatId,paramData);
    }

    /**
     * 删除商品参数模版
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteItemParamById")
    public Integer deleteItemParamById(@RequestParam Integer id){
        return itemParamService.deleteItemParamById(id);
    }

    /**
     * 首页查询分类
     *
     * @return
     */
    @RequestMapping("selectItemCategoryAll")
    public CatResult selectItemCategoryAll(){
        return itemParamService.selectItemCategoryAll();
    }

    /**
     * 根据id查询商品描述
     *
     * @return
     */
    @RequestMapping("selectItemDescByItemId")
    public TbItemDesc selectItemDescByItemId(@RequestParam Long itemId){
        return itemParamService.selectItemDescByItemId(itemId);
    }

    /**
     * 根据id 查询商品分类信息
     *
     * @param itemId
     * @return
     */
    @RequestMapping("selectTbItemParamItemByItemId")
    public TbItemParamItem selectTbItemParamItemByItemId(@RequestParam Long itemId){
        return itemParamService.selectTbItemParamItemByItemId(itemId);
    }
}
